/**
 * Created by Administrator on 2016/10/18.
 */
var msg = new Object()
var info = new Object()
var orderId  = $("input[name='order-id']").val();
$(document).ready(function(){
    getMsg('.taocan-item')
    getInfo()
    Sum()
})
//加号绑定事件
$('.plus').on('touchend',function(){
    var wrapper = $(this).parents('.taocan-item')
    var ID = wrapper.attr('data-id')
    var name = $(wrapper).find('.taocan-name').text()
    creatData(ID,wrapper)
    if(msg[ID].num == 0){
        $(this).prevAll('.minus').show()
        $(this).prev('.num').show()
    }
    msg[ID].num++
    $(this).prev('.num').text(msg[ID].num)
    Remove(ID)  //判断购物车中是否存在该数据，清空原有购物车中数据
    console.log(msg[ID].num*msg[ID].show)
    addDom(ID,name,msg[ID].pri*msg[ID].num,msg[ID].num,msg[ID].num*msg[ID].show) //生成购物车DOM
    save()
    Sum()
})
//减号绑定事件
$('.minus').on('touchend',function(){
    var wrapper = $(this).parents('.taocan-item')
    var ID = wrapper.attr('data-id')
    var name = $(wrapper).find('.taocan-name').text()
    creatData(ID,wrapper)
    Remove(ID) //判断购物车中是否存在该数据，清空原有购物车中数据
    msg[ID].num--
    if(msg[ID].num == 0){
        $(this).hide()
        $(this).next('.num').text('0').hide()
        delete msg[ID]

    }else{
        $(this).next('.num').text(msg[ID].num)
        addDom(ID,name,msg[ID].pri*msg[ID].num,msg[ID].num,msg[ID].num*msg[ID].show) //生成购物车DOM
    }
    save()
    Sum()
})
function creatData(ID,wrapper){
    var name = $(wrapper).find('.taocan-name').text()
    var num = $(wrapper).find('.num').text()
    var pri = $(wrapper).attr('data-pri')
    var show = $(wrapper).attr('data-show')
    msg[ID] = {
        'pri' : pri,
        'show' : show,
        'num' : num,
        'name' : name
    }
}
//储存sessionStorage数据
function save(){
    sessionStorage['taocan' + orderId] = JSON.stringify(msg);
}
//获取sessionStorage msg(套餐)数据
function getMsg(target){
    if(sessionStorage['taocan' + orderId] != undefined){
        msg = JSON.parse(sessionStorage['taocan' + orderId])
        for(x in msg){
            var taocan = $(target).filter(function(){
                return $(this).attr('data-id') == x
            })
            taocan.find('.num').text(msg[x].num)
            taocan.find('.minus').show()
            taocan.find('.num').show()
            addDom(x,msg[x].name,msg[x].pri*msg[x].num,msg[x].num,msg[x].num*msg[x].show) //生成购物车DOM
        }
    }
}
//获取sessionStorage info(app)数据 并展示
function getInfo(){
    if(sessionStorage['app'+orderId] != undefined && sessionStorage['app'+orderId] != '{}'){
        info = JSON.parse(sessionStorage['app'+orderId])
        var sum = 0
        var Pri = 0
        var Cpm = 0
        for(x in info){
            if(info[x].num != 0){
                addAppDom(x,info[x].name,info[x].pri*info[x].num,info[x].num,info[x].num*0.1) //生成购物车DOM
                sum += parseInt(info[x].num,10)
                Pri += info[x].num*info[x].pri
                Cpm += parseInt((0.1*info[x].num).toFixed(1),10)
            }
        }
        $('.price span').text(Pri)
        $('.show-number').text(Cpm)
        if(sum != 0){
            $('.shopcart').attr('data-attr',sum).addClass('full').removeClass('none')
            $('#pushButton').removeClass('none')
        }else{
            $('.shopcart').removeAttr('data-attr','').removeClass('full').addClass('none')
            $('#pushButton').addClass('none')
        }
    }
}
//购物车弹出框交互
$('.shopcart').on('click',function(){
    if($(this).hasClass('full') && $('.cart-detail').hasClass('display')){
        $('#darkBack').addClass('background')
        $('.toolbar').addClass('top')
        $('.cart-detail').removeClass('display').animate({
            bottom : 44
        },500)
        $('.side-content').css({'overflow':'hidden'})
    }else if($('#darkBack').hasClass('background') && !$('.cart-detail').hasClass('display')){
        hideCart()
    }
})
function hideCart() {
    $('.cart-detail').animate({
        bottom : -100
    },300,function(){
        $('#darkBack').removeClass('background')
        $('.cart-detail').addClass('display')
        $('.toolbar').removeClass('top')
        $('.side-content').css({'overflow':'scroll'})
        var emptyItem = $('.cart-item+.cart-item').filter(function(){
            return $(this).find('input').val() == 0
        })
        if(emptyItem){
            emptyItem.remove()
            for(x in msg){
                if(msg[x].num == 0){
                    delete msg[x]
                }
            }
            for(x in info){
                if(info[x].num == 0){
                    delete info[x]
                }
            }
        }
        save()
        sessionStorage['app'+orderId] = JSON.stringify(info)
    })
}
//购物车收回交互
$('#darkBack').on('click',function(){
    if($(this).hasClass('background') && !$('.cart-detail').hasClass('display')){
        hideCart()
    }
})
//购物车弹出框输入框交互
$('.cart-item input').on('keyup',function(){
    var itemVal = $(this).val()
    var itemWrapper = $(this).parent().parent()
    var itemCpm = itemWrapper.attr('item-cpm')
    var itemid = itemWrapper.attr('item-id')
    var appid = itemWrapper.attr('app-id')
    //改变对应app列表数据
    if(itemid){
        var taocan = $('.taocan-item').filter(function(){
            return $(this).attr('data-id') == itemid
        })
        taocan.find('.num').text(itemVal)
        if(itemVal == 0){
            taocan.find('.minus').hide()
            taocan.find('.num').hide()
            msg[itemid].num = 0
        }else {
            taocan.find('.minus').show()
            taocan.find('.num').show()
            msg[itemid].num = itemVal
        }
        //改变弹出框数据
        itemWrapper.find('.item-show').text(msg[itemid].num*msg[itemid].show)
        itemWrapper.find('.item-price').text(msg[itemid].num*msg[itemid].pri)
    }else if(appid){
        if(itemVal == 0){
            info[appid].num = 0
        }else {
            info[appid].num = itemVal
        }
        itemWrapper.find('.item-show').text((itemVal*0.1).toFixed(1))
        itemWrapper.find('.item-price').text(itemVal*info[appid].pri)
        sessionStorage['app'+orderId] = JSON.stringify(info)
    }
    //计算工具栏中的总数据
    save()
    Sum()
})
//清空单个垃圾桶交互
$('.cart-item span.m-lajitong').on('click',function(){
    var itemWrapper = $(this).parent()
    var itemid = itemWrapper.attr('item-id')
    var appid = itemWrapper.attr('app-id')
    var taocan = $('.taocan-item').filter(function(){
        return $(this).attr('data-id') == itemid
    })
    //清空该表单内容
    itemWrapper.find('.item-price').text(0)
    itemWrapper.find('.item-show').text(0)
    itemWrapper.find('input').val(0)
    //清楚欲投放的app的信息
    if(appid != undefined){
        info[appid].num = 0
        sessionStorage['app'+orderId] = JSON.stringify(info)
    }else {
        msg[itemid].num = 0
    }
    //清空对应app列表数据
    taocan.find('.num').text('0').hide()
    taocan.find('.minus').hide()
    save()
    Sum()
})
//清空购物车交互
$('.cart-title-right').on('click',function(){
    var empty = confirm('是否要清空购物车')
    if(empty == true){
        $('.num').text('')
        $('.minus').hide()
        for (x in msg){
            delete msg[x]
        }
        for (x in info){
            delete info[x]
            sessionStorage['app'+orderId] = JSON.stringify(info)
        }
        $('.cart-detail').animate({
            bottom : -100
        },300,function(){
            $('#darkBack').removeClass('background')
            $('.toolbar').removeClass('top')
            $('.cart-detail').addClass('display')
            $('.cart-item+.cart-item').remove()
            $('.side-content').css({'overflow':'scroll'})
        })
        save()
        Sum()
    }
})
//更多按钮
$('.more').on('touchend',function(){
    var that = this
    if($(this).hasClass('less')){
        $(that).parents('.taocan-inner').next().addClass('display')
        $(that).html(' 更多详情 <span class=" m-xiangxiajiantou m-icon"></span>')
        
    }else{
        $(that).parents('.taocan-inner').next().removeClass('display')
        $(that).html(' 收起详情 <span class=" m-up-arrow m-icon"></span>')

    }
    $(that).toggleClass('less')
})
//投放按钮
$('#pushButton').on('click',function(){
    if($(this).hasClass('none')){
        alert('请选择套餐投放')
    }else{
        $('.choose-push').removeClass('display')
        $('#darkBack').addClass('background')
        $('.cart-detail').animate({
            bottom : -100
        },300,function(){
            $('.cart-detail').addClass('display')
            $('.toolbar').removeClass('top')
        })
    }
})
$('.choose-push .no').on('click',function(){
    $('#darkBack').removeClass('background')
    $('.choose-push').addClass('display')
})
$('.choose-push .yes').click(function(){
    var orderId  = $("input[name='order-id']").val();
    ajax_data(orderId)
})
//移除购物车dom
function Remove(id){
    var taocanName = $('.cart-item').filter(function(){
        return $(this).attr('item-id') == id
    })
    if(taocanName){
        taocanName.remove()
    }
}
function addDom(id,str,appPri,appNum,cpm){
    //往购物车中添加该app信息
    var dom = $('.cart-item-list>.cart-item:first-child').clone(true,true).appendTo('.cart-item-list')
        .attr('item-id',id).attr('item-name',str)
    dom.find('.item-name').text(str)
    dom.find('.item-price').text(appPri)
    dom.find('.item-show').text(cpm)
    dom.find('input').val(appNum)
}
function addAppDom(id,str,appPri,appNum,cpm){
    //往购物车中添加该app信息
    var dom = $('.cart-item-list>.cart-item:first-child').clone(true,true).appendTo('.cart-item-list')
        .attr('app-id',id).attr('item-name',str)
    dom.find('.item-name').text(str)
    dom.find('.item-price').text(appPri)
    dom.find('.item-show').text(cpm.toFixed(1))
    dom.find('input').val(appNum)
}
function Sum(){
    var sum = 0
    var Pri = 0
    var Cpm = 0
    //套餐求和
    for(x in msg){
        sum += parseInt(msg[x].num,10)
        Pri += msg[x].num*msg[x].pri
        Cpm += msg[x].num*msg[x].show
    }
    //app求和
    for(x in info){
        if(info[x].num != 0){
            sum += parseInt(info[x].num,10)
            Pri += info[x].num*info[x].pri
            Cpm += parseInt((0.1*info[x].num).toFixed(1),10)
        }
    }
    $('.price span').text(Pri)
    $('.show-number').text(Cpm)
    if(sum != 0){
        $('.shopcart').attr('data-attr',sum).addClass('full').removeClass('none')
        $('#pushButton').removeClass('none')
    }else{
        $('.shopcart').removeAttr('data-attr','').removeClass('full').addClass('none')
        $('#pushButton').addClass('none')
    }
}